<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Autonym;
use App\Models\RechOrder;
use App\Models\Recharge;
use App\Models\Unbind;
use App\Models\Currency;
use App\Models\Enter;
use Exception;
use DB;

/**
 * 法币交易相关
 */
class LegalController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'legal');
    }

    /**
     * 交易挂单记录
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function rech_order_list(Request $request)
    {
        //会员ID
        $account = $request->get('account');
        
        //条件查询
        $data = RechOrder::where(function ($query) use ($account) {
            if (!empty($account)) {
                //根据账号查找商家id
                $busiIds = User::where(['type' => 2, 'attestation'=> 2])
                    ->where('account', 'like', '%'.$account.'%')
                    ->pluck('id');
                //dd($busiIds);
                $query->whereIn('uid', $busiIds);
            }
        })->orderBy('created_at', 'desc')->paginate(10);
        //分页索引
        $page_num = $data->firstItem();

        return view('admin/legal/rech_order_list', compact('data', 'account', 'page_num'));
    }
    /**
     * 交易记录
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function recharge_list(Request $request)
    {
        //会员ID
        $account = $request->get('account');
        //状态值
        $status = $request->get('status');
        //根据账号查找商家id
        $uids = User::where('account', 'like', '%'.$account.'%')->pluck('id');

        //条件查询
        $data = Recharge::where(function ($query) use ($uids) {
            $query->whereIn('bid', $uids);
        })->where(function ($query) use ($status) {
            if ($status != 99 && isset($status)) {
                $query->where('status', $status);
            }
        })->orderBy('created_at', 'desc')->paginate(10);
        //分页索引
        $page_num = $data->firstItem();

        return view('admin/legal/recharge_list', compact('data', 'account', 'page_num', 'status'));
    }

    //撤销订单
    public function cancel_rech_order(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');

        $rechOrder = RechOrder::find($id);

        $rechOrder->update(['status' => 5]);

        return back()->with(['status' => 1, 'msg' => '撤销成功']);
    }

    //注销商家
    public function unbind_business(Request $request)
    {
        $uid = $request->get('uid');

        $user = User::find($uid);

        $unbind = Unbind::where('uid', $uid)->first();

        //商家是否有正在进行的交易单
        $orders = Recharge::where(['bid' => $uid])->whereIn('status', [2, 5])->get();

        if (collect($orders)->count() > 0) {

            return back()->with(['status' => 0, 'msg' => '有未完成的交易存在，不能注销']);
        }

        //取消该商家所有广告单
        $rechs = RechOrder::where('uid', $uid)->get();

        if (collect($rechs)->count() > 0) {
            
            RechOrder::where('uid', $uid)->update(['status' => 5]);
        }

        $unbind->update(['status' => 2]);

        $user->update(['type' => 1]);
        //删除商家申请
        Enter::where('uid',$uid)->delete();
        return back()->with(['status' => 1, 'msg' => '注销成功']);
    }
}
